<?php
class ImageCreater{

	//图片对象
	private $backroundImg;//背景图片
	private $sourceImg;//添加的图片
	private $qrCode;//添加的二维码
	private $font;//字体
	
	//图片路径
	private $backroundImgPath;//背景图片路径
	private $sourceImgPath;//添加的图片路径
	private $qrCodePath;//添加的二维码路径


	/**
	 * @param $backroundImgPath 背景图片路径路径
	 * @param $sourceImgPath 要添加的图片路径
	 * @param $qrCodePath    要天剑的二维码路径
	 */
	function __construct($backroundImgPath,$sourceImgPath,$qrCodePath){
		//赋值图片路径
		$this -> backroundImgPath = $backroundImgPath;
		$this -> sourceImgPath = $sourceImgPath;
		$this -> qrCodePath = $qrCodePath;
		
		//赋值图片对象
		$this -> backroundImg = imagecreatefromstring(file_get_contents($backroundImgPath));
		$this -> sourceImg = imagecreatefromstring(file_get_contents($sourceImgPath));
		$this -> qrCode = imagecreatefromstring(file_get_contents($qrCodePath));
	}

	/*
	
	 * 生成图片
	
	 */
	public function createImage(){
		imagecopymerge($this -> backroundImg, $this -> sourceImg, 50, 250, 0, 0, 500, 300, 100);
		imagecopymerge($this -> backroundImg, $this -> qrCode, 469, 571, 0, 0, 128, 128, 100);
	}


	/*
	
	 * 显示图片
	
	 */
	public function showImage(){
		list($dst_w, $dst_h, $dst_type) = getimagesize($this -> backroundImgPath);
		switch ($dst_type) {
	    	case 1://GIF
	        	header('Content-Type: image/gif');
	        	imagegif($this -> backroundImg);
	        	break;
	    	case 2://JPG
	        	header('Content-Type: image/jpeg');
	        	imagejpeg($this -> backroundImg);
	        	break;
	    	case 3://PNG
	        	header('Content-Type: image/png');
	        	imagepng($this -> backroundImg);
	        	break;
	    	default:
	        	break;
		}	
	}
	
	/**
	 * @param $str 添加的字符
	 * @param $font 字体的路径
	 * @param $qrCodePath    要天剑的二维码路径
	 */
	public function addFont($size,$angle,$x,$y,$font,$str){
		//打上文字
		$black = imagecolorallocate($this -> backroundImg, 43, 40, 165);//字体颜色
//		imagettftext($this -> backroundImg, 47, 0, 100 ,100, $black, $font, $str);
		
		imagefttext($this -> backroundImg, $size, $angle, $x, $y, $black, $font, $str);
	}
	
	
	public function setFont($font_url){
		$this -> font = $font_url;
	}
	
	public function getFont(){
		return $this -> font;
	}
	
	/*

 	* 消除图片

 	*/
	public function destroyImage(){
		imagedestroy($this -> backroundImg);
		imagedestroy($this -> sourceImg);
		imagedestroy($this -> qrCode);
	}
	
	
	
}
?>